package com.xinqi.controller;

import com.xinqi.common.R;
import com.xinqi.common.ResponseEnum;
import com.xinqi.pojo.bean.User;
import com.xinqi.pojo.vo.LoginVo;
import com.xinqi.pojo.vo.UserInfoVo;
import com.xinqi.service.UserService;
import com.xinqi.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xin麒
 * @date 2023/1/14 20:21
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @GetMapping("/hello")
    public R<String> hello(){
        return R.success("hello");
    }

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public R<User> register(@RequestBody User user){
        return userService.register(user);}

    @PostMapping("/login")
    public R<UserInfoVo> login(@RequestBody LoginVo loginVo){
        String mobile = loginVo.getMobile();
        if (mobile == null) return R.error("手机号输入错误");
        String password = loginVo.getPassword();
        if (password == null) return R.error("password输入错误");

        return userService.login(loginVo);
    }

    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);

        if (!result) return R.error(ResponseEnum.LOGIN_AUTH_ERROR);
        return R.ok();
    }

    @GetMapping("/other")
    public R<String> other(){
        return R.success("other");
    }
}
